#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <string>

using namespace std;

#define RANDRANGE(A, B) ((rand() % B) + A)

const int difStringCount = 10000000; // 字符串个数
const int seqMaxIndex = 10; // 访问字符串个数

void randstr(char *str, const int len, const int seed) 
{
    srand(seed);
    for(int i = 0; i < len; ++i) {
        switch(rand() % 2) {
        case 1:
            str[i] = 'A' + rand() % 26;
            break;
        case 2:
            str[i] = 'a' + rand() % 26;
            break;
        default:
            str[i] = '0' + rand() % 10;
            break;
        }
    }
    str[len] = '\0';
}

void make_test_file_alldiff()
{
    cout << "make_test_file()" << endl;
    char str[4096];
    int byte[] = {8, 16, 32, 64, 128};
    for(int a : byte) {
        fstream fs("test_dif_100w_" + to_string(a) + ".txt", ios::app);
        if(!fs.good()) {
            cerr << ">> fstream open file error" << endl;
        }       
        for(int i = 0; i < difStringCount; ++i) {
            randstr(str, a, i);
            fs << str << endl;
        }
    }
}

void make_test_file_emulated(const int percent)
{ // 产生percent%重复的字符串，不能超过50%
    cout << "make_test_file()" << endl;
    char str[4096];
    int byte[] = {8};//, 16, 32, 64, 128};
    for(int a : byte) {
        fstream fs("test_emulated_" + to_string(percent) + "%_1000w_" + to_string(a) + ".txt", ios::app);
        if(!fs.good()) {
            cerr << ">> fstream open file error" << endl;
        }       
        for(int i = 0; i < difStringCount * percent * 0.01; ++i) {
            randstr(str, a, i);
            fs << str << endl;
        }
        for(int i = 0; i < difStringCount * (100 - percent) * 0.01; ++i) {
            randstr(str, a, i);
            fs << str << endl;
        }
    }
}

void make_get_sequence(const int count)
{
    cout << "make_get_sequence()" << endl;
    fstream fs("test_seq.txt", ios::app);
    if(!fs.good()) {
        cerr << ">> fstream open file error" << endl;
    }

    srand((unsigned int)time(0));
    for(int i = 0; i < count; ++i) {
        fs << rand() % seqMaxIndex << endl;
    }
}

void GetAccessSequence(const char* filename) {
    std::vector<int> seqs;

    std::fstream fs;
    fs.open(filename, std::fstream::in);
    if(!fs.good()) {
        cerr << ">> fstream open file error" << endl;
    }
    char buf[100];
    int seq;
    while(fs.getline(buf, sizeof(buf) - 1).good()) {
        seq = atoi(buf);
        cout << seq << " ";
    }
    cout << endl;
}

int main()
{
    make_test_file_emulated(50); //生成有%多少重复度的测试数据
    // make_get_sequence(100000);
    // GetAccessSequence("test_seq.txt");
    return 0;
}